<template>
  <Table
    ref="tableRef"
    :url="URL_PAGINGNOTICE"
    :columns="data.columns"
    :handles="data.handles"
    :toolbar="data.toolbar"
    :query="data.query"
  ></Table>
</template>

<script lang="ts" setup name="noticeList">
import Table from '@/components/table/index.vue';
import { URL_PAGINGNOTICE, deleteNoticeById } from '@/service/apis/market';
import { NOTICE_METHOD, NOTICE_STATUS } from '@/utils/dictionaries';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

const data = reactive({
  columns: [
    {
      prop: 'noticeTitle',
      label: '通知标题',
    },
    {
      prop: 'sendingStatus',
      label: '发送状态',
      dic: NOTICE_STATUS,
    },
    {
      prop: 'sendingMethod',
      label: '发送方式',
      dic: NOTICE_METHOD,
    },
    {
      prop: 'sendingTime',
      label: '发送时间',
    },
    {
      prop: 'updateTime',
      label: '修改时间',
    },
  ],
  handles: [
    {
      label: '详情',
      handle: (row: any) => {
        console.log('详情 ---', row);
        router.push({
          name: 'noticeDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '修改',
      permission: 'noticeList.edit',
      disabledFun: (row: any) => {
        return row.sendingStatus === '1';
      },
      handle: (row: any) => {
        console.log('编辑---', row);
        router.push({
          name: 'noticeEdit',
          query: { id: row.id },
        });
      },
    },
    {
      label: '删除',
      color: '#FA3939',
      permission: 'noticeList.delete',
      disabledFun: (row: any) => {
        return row.sendingStatus === '1';
      },
      handle: (row: any) => {
        ElMessageBox.confirm('删除后的消息将不再发送，是否继续？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          deleteNoticeById({
            id: row.id,
          }).then((res: any) => {
            tableRef.value.getData();
          });
        });
      },
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'noticeList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'noticeAdd',
        });
      },
    },
  ],
  query: [
    {
      label: '通知标题',
      prop: 'noticeTitle',
      comName: 'input',
      props: {
        placeholder: '请输入通知标题',
      },
    },
    {
      label: '发送状态',
      prop: 'sendingStatus',
      comName: 'select',
      props: {
        placeholder: '请选择发送状态',
        change: (value: any) => {
          console.log(value);
        },
        options: NOTICE_STATUS,
      },
    },
    {
      label: '发送方式',
      prop: 'sendingMethod',
      comName: 'select',
      props: {
        placeholder: '请选择发送方式',
        change: (value: any) => {
          console.log(value);
        },
        options: NOTICE_METHOD,
      },
    },
  ],
});
</script>